/**
 *
 * Copyright (C) 2008  Igor Vdovichenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Though a sincere effort has been made to deliver a professional,
 * quality product,the library itself is distributed WITHOUT ANY WARRANTY;
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 */
package com.vin.scriptutils.utils;

import java.util.Arrays;
import java.util.Collection;

/**
 *
 * Translate string to escaped string
 * 
 * @author igor
 */
public class EscapedString {

    private final String str;
    private static final char escChr = '\\';
    private static final Collection<Character> charactresToEscape =
            Arrays.asList(' ', '|', '&', '^',';', ':', ',',
            '\t', '\"', '\\',
            '!', '@', 
            '[', '{', ']', '}', '(', ')', '<', '>',
            '#', '*',
            '$', '?');

    public EscapedString() {
        this.str = null;
        
    }

    public EscapedString(String str) {
        this.str = getString(str);
    }

    public String getString() {
        return this.str;
    }

    public static String getString(String str) {
        if (null == str) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (charactresToEscape.contains(c)) {
                result.append(escChr);
            }
            result.append(c);
        }
        return result.toString();
    }
}
